﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;

namespace CMD.BAL.AUTH
{
    [DataContractAttribute]
    public class User
    {
        [DataMemberAttribute]
        public int idUsr;
        [DataMemberAttribute]
        public string mail;
        [DataMemberAttribute]
        public string name;
        [DataMemberAttribute]
        public List<Rol> roles;
        [DataMemberAttribute]
        public string wUsr;
        public static CMD.BAL.MISC.DeleteStateMsg Delete(int[] aIdUsr)
        {
            CMD.BAL.MISC.DeleteStateMsg delete;
            //convirto
            string join = String.Join(", ", aIdUsr);    
            //ejecuto el store
            try
            {
                int dt = CMD_DKT.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_DELETEUSER'" + join + "'");
                delete = new MISC.DeleteStateMsg("registros eliminados " + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new MISC.DeleteStateMsg("error al eliminar" + ex, false);
                return delete;
            }
        }
        public static List<User> GetData(int[] aIdUsr,bool getRolList)
        {
            string join = String.Join(", ", aIdUsr);
            DataTable dt = CMD_DKT.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETUSER'" + join + "','" + getRolList + "'");
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                List<User> list = dt.AsEnumerable().Select(
                    row => new User
                    {
                        idUsr = row.Field<int?>(0).GetValueOrDefault(),
                        mail = String.IsNullOrEmpty(row.Field<string>(2))
                        ? ""
                        : row.Field<string>(2),
                        name = String.IsNullOrEmpty(row.Field<string>(1))
                        ? ""
                        : row.Field<string>(1),
                        wUsr = String.IsNullOrEmpty(row.Field<string>(3))
                        ? ""
                        : row.Field<string>(3),
                    }
                    ).ToList();
                return list;
            }
        }
        public static List<User> GetDataByIdRol(string[] aIsRol, bool getRolList)
        {
            string join = String.Join(", ", aIsRol);
            DataTable dt = CMD_DKT.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETUSER'" + join + "','" + getRolList + "'");
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                List<User> list = dt.AsEnumerable().Select(
                    row => new User
                    {
                        //idRol= String.IsNullOrEmpty(row.Field<string>(0))
                        //? ""
                        //: row.Field<string>(0),
                        idUsr = row.Field<int?>(0).GetValueOrDefault(),
                        mail = String.IsNullOrEmpty(row.Field<string>(2))
                        ? ""
                        : row.Field<string>(2),
                        name = String.IsNullOrEmpty(row.Field<string>(1))
                        ? ""
                        : row.Field<string>(1),
                        wUsr = String.IsNullOrEmpty(row.Field<string>(3))
                        ? ""
                        : row.Field<string>(3),
                    }
                    ).ToList();
                return list;
            }
        }
        public static CMD.BAL.MISC.UpsertStateMsg Upsert(User usr)
        {
            CMD.BAL.MISC.UpsertStateMsg insertUser;
            try
            {
                int dt = CMD_DKT.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_UPSERTUSER'" + usr + "'");
                insertUser = new MISC.UpsertStateMsg("registros guardados " + dt.ToString(), true);
                return insertUser;

            }
            catch (Exception ex)
            {
                insertUser = new MISC.UpsertStateMsg("error al guardar " + ex, false);
                return insertUser;
            }
        }
        public User() { }
        public User(int idUsr,string mail, string name,string wUsr) 
        {
            this.idUsr = idUsr;
            this.mail = mail;
            this.name = name;
            this.wUsr = wUsr;
        }
    }
}
